package com.leyou.auth.config;

import com.leyou.auth.utils.JwtConstans;
import com.leyou.auth.utils.RsaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.nio.file.Files;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author 岩冰
 * @version 1.0
 * @date:2018/10/18
 */
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties {

    private String secret;

    private String pubKeyPath;
    private String priKeyPath;
    private Integer expire;

    private PublicKey publicKey; // 公钥

    private PrivateKey privateKey; // 私钥

    private String cookieName;
    private Integer cookieMaxAge;

    public PublicKey getPublicKey() {
        return publicKey;
    }

    public JwtProperties setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
        return this;
    }

    public PrivateKey getPrivateKey() {
        return privateKey;
    }

    public JwtProperties setPrivateKey(PrivateKey privateKey) {
        this.privateKey = privateKey;
        return this;
    }

    public String getCookieName() {
        return cookieName;
    }

    public JwtProperties setCookieName(String cookieName) {
        this.cookieName = cookieName;
        return this;
    }

    public Integer getCookieMaxAge() {
        return cookieMaxAge;
    }

    public JwtProperties setCookieMaxAge(Integer cookieMaxAge) {
        this.cookieMaxAge = cookieMaxAge;
        return this;
    }

    private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);
    @PostConstruct
    public void init() {
        try {
            File publicPath = new File(pubKeyPath);
            File privatePath = new File(priKeyPath);

            if (!publicPath.exists()||!privatePath.exists()) {
            RsaUtils.generateKey(pubKeyPath,priKeyPath,secret);
            }
            this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
            this.privateKey = RsaUtils.getPrivateKey(priKeyPath);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("初始化创建公私药错误",e);
            throw new RuntimeException();
        }

    }
    public String getSecret() {
        return secret;
    }

    public JwtProperties setSecret(String secret) {
        this.secret = secret;
        return this;
    }

    public String getPubKeyPath() {
        return pubKeyPath;
    }

    public JwtProperties setPubKeyPath(String pubKeyPath) {
        this.pubKeyPath = pubKeyPath;
        return this;
    }

    public String getPriKeyPath() {
        return priKeyPath;
    }

    public JwtProperties setPriKeyPath(String priKeyPath) {
        this.priKeyPath = priKeyPath;
        return this;
    }

    public Integer getExpire() {
        return expire;
    }

    public JwtProperties setExpire(Integer expire) {
        this.expire = expire;
        return this;
    }
}
